﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DropBoxToDo.UserControls;
using TasksDataXml;
using TasksDataXml.Data;
using TasksDataXml.Exceptions;

namespace DropBoxToDo
{
    public partial class MainForm : Form
    {
        private readonly Controller _controller;
        private CategoryContainer _categories;

        public MainForm()
        {
            _controller = new Controller("");
            InitializeComponent();
            LoadCategories();
            LoadCategoriesView();
            LoadTasksView();
            CheckReminders();

            emptyTaskControl.OnNewTaskCreated = OnNewTaskCreated;
        }

        private void LoadCategories()
        {
            CommonException ex;
            _categories = _controller.LoadData(out ex, null);
        }

        private void LoadCategoriesView()
        {
            if (_categories == null)
                return;
            foreach (Category category in _categories.CategoryList)
            {
                LoadCategoryTree(category, treeCategories.Nodes);
            }
        }

        private void LoadCategoryTree(Category category, TreeNodeCollection nodes)
        {
            var node = new TreeNode(category.Name) { Tag = category };
            nodes.Add(node);

            if (category.CategoryList != null)
            {
                foreach (Category c in category.CategoryList)
                {
                    LoadCategoryTree(c, node.Nodes);
                }
            }
        }

        private void LoadTasksView()
        {
            if (_categories == null)
                return;

            if (treeCategories.Nodes.Count == 0)
                return;

            TreeNode node = treeCategories.SelectedNode;
            if (node == null)
                node = treeCategories.Nodes[0];

            var category = node.Tag as Category;
            if (category == null)
                return;

            tasksPanel.Controls.Clear();
            FillTaskViewByCategory(category);
        }

        private void FillTaskViewByCategory(Category category)
        {
            foreach (Task Task in category.Tasks)
            {
                TaskControl tsk = TaskControl.CreateTask(Task);
                tasksPanel.Controls.Add(tsk);
            }

            if (category.CategoryList.Count > 0)
            {
                foreach (Category cat in category.CategoryList)
                {
                    FillTaskViewByCategory(cat);
                }
            }
        }

        private void CheckReminders()
        {
            if (_categories == null)
                return;
        }

        private bool OnNewTaskCreated(string s)
        {
            if (treeCategories.Nodes.Count < 1)
                return false;

            TreeNode node = treeCategories.SelectedNode;
            if (node == null)
                node = treeCategories.Nodes[0];

            var cat = node.Tag as Category;
            CommonException ex;
            Task newTask = _controller.AddTask(cat, s, out ex);

            if (newTask != null && ex == null)
            {
                TaskControl t = TaskControl.CreateTask(newTask);
                tasksPanel.Controls.Add(t);
                return true;
            }

            return false;
        }

        private void AddCategoryNode()
        {
            TreeNode selectedNode = treeCategories.SelectedNode;
            Guid parenGuid = Guid.Empty;
            if (selectedNode != null)
            {
                var cat = selectedNode.Tag as Category;
                if (cat != null)
                    parenGuid = cat.GuidId;
            }
            TreeNodeCollection nodes = selectedNode != null ? selectedNode.Nodes : treeCategories.Nodes;

            CommonException ex;
            Category newCat = _controller.AddCategory(parenGuid, out ex);
            var node = new TreeNode(newCat.Name) {Tag = newCat};
            nodes.Add(node);
        }

        private void DeleteNode(TreeNode node)
        {
            if (node == null)
                return;

            var cat = node.Tag as Category;
            CommonException ex;
            _controller.DeleteCategory(cat, out ex);
            node.Remove();
        }

        private static TreeNode GetNodeByPoint(Point point, TreeView treeView)
        {
            point = treeView.PointToClient(point);
            TreeNode node = treeView.GetNodeAt(point);
            return node;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }
    }
}